package com.RWM.activities;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.RWM.helpers.CustomOnItemSelectedListener;
import com.RWM.helpers.VenmoSDK;
import com.RWM.reference.Reference.RequestMethod;
import com.RWM.serverTools.Connect;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

public class VenmoActivity extends Activity implements OnClickListener {
	private Intent myIntent;
	private RadioGroup radiog;
	private RadioButton pay;
	private RadioButton charge;
	private int pzoneID;
	private Connect connect;
	private Spinner spinner1;
	private String phone;
	public CustomOnItemSelectedListener customOnItemSelectedListener;
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		// Set View to venmo.xml
		setContentView(R.layout.venmo);

		// Get parking zone ID from intent
		myIntent = getIntent();
		Bundle extras = myIntent.getExtras();
		pzoneID = extras.getInt("pzoneID");
		Toast.makeText(VenmoActivity.this, "ZONEID IS"+pzoneID ,
				Toast.LENGTH_LONG).show();

		//Receive and parse JSONArray of zone info from database
		connect = new Connect("http://m.cip.gatech.edu/developer/imf/api/RWMSERVER/drivers/");
		connect.AddParam("pzoneID", Integer.toString(pzoneID));
		try {
			connect.Execute(RequestMethod.POST);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		JSONArray driverlist = null;
		// Adding spinner button
		spinner1 = (Spinner) findViewById(R.id.spinner_drivers);
		List<String> list = new ArrayList<String>();
		try {

			driverlist = new JSONArray(connect.getResponse());
			// Populating spinner with username of drivers
			int i,size = driverlist.length();
			for (i=0; i<size; i++){
				list.add(driverlist.getJSONObject(i).getString("username")); //Access the username item in each array object
			} 
		} 
		catch (JSONException e) {
			Log.v("json", "Could not create JSONArray");
			list.add("No Drivers");
		}

		/***************************************************************************/
		//NOw that we have the list, parse out the names an place them in
		/*
		 	Toast.makeText(VenmoActivity.this, connect.getResponse() ,
					Toast.LENGTH_LONG).show();
		 */
		/***************************************************************************/

		ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, list);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner1.setAdapter(dataAdapter);
		customOnItemSelectedListener = new CustomOnItemSelectedListener(driverlist);
		spinner1.setOnItemSelectedListener(customOnItemSelectedListener);
		
	
		// Get buttons
		Button proceed = (Button) findViewById(R.id.btn_venmo_accept);
		Button back = (Button) findViewById(R.id.btn_venmo_back);
		radiog = (RadioGroup) findViewById(R.id.radioGroup);
		pay = (RadioButton) findViewById(R.id.payRadio);
		charge = (RadioButton) findViewById(R.id.chargeRadio);

		proceed.setOnClickListener(this);
		back.setOnClickListener(this);
	}
	

	public void onClick(View v) {
		switch (v.getId()){

		//proceed to venmo site
		case R.id.btn_venmo_accept:

			String app_id="1125";
			String local_app_id="abcd";
			String app_name="Ride With Me";
			String recipient;
			String amount;
			String note;
			String txn;

			int selected = radiog.getCheckedRadioButtonId();
			pay = (RadioButton) findViewById(selected);
			txn=(String) pay.getText();
			EditText amountText = (EditText) findViewById(R.id.venmo_amount);
			EditText noteText = (EditText) findViewById(R.id.venmo_note);
			note=noteText.getText().toString();
			amount=amountText.getText().toString();

			phone = customOnItemSelectedListener.getPhone();
			Intent sendIntent = VenmoSDK.openVenmoPayment(app_id, local_app_id, "Ride With Me", phone, amount, note, txn);
			try {
				startActivity(sendIntent);
			}
			catch (ActivityNotFoundException e) // Exception thrown when Venmo native app not install on device, so fallback to web version
			{
				sendIntent = VenmoSDK.openVenmoPaymentInBrowser(app_id, local_app_id, "Ride With Me", phone, amount, note, txn);
				startActivity(sendIntent);
			}

			break;

			//return to previous activity
		case R.id.btn_venmo_back:
			finish();
			break;
		}

	}

}
